package com.jcommander.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileSystemUtils;
import org.apache.commons.io.FileUtils;

public class FileSystemNode {

	private final File parent;
	private final File file;
	private final List<File> children = new ArrayList<File>();
	private final FileSystemRow fileSystemRow;
	private final boolean rootFile;
	private final boolean leafFile;
	
	public FileSystemNode(File file) {
		this.file = file;
		if (file.getParentFile() == null) {
			rootFile = true;
			parent = null;
		} else {
			rootFile = false;
			parent = file.getParentFile(); 
		}
		
		if (!file.isFile()){
			leafFile = false;
			refreshChildrens();
		} else {
			leafFile = true;
		}
		fileSystemRow = new FileSystemRow(file);
	}

	public File getParent() {
		return parent;
	}

	public File getFile() {
		return file;
	}

	public List<File> getChildren() {
		refreshChildrens();
		return children;
	}

	public FileSystemRow getFileSystemRow() {
		return fileSystemRow;
	}

	public boolean isRootFile() {
		return rootFile;
	}

	public boolean isLeafFile() {
		return leafFile;
	}
	
	
	private void refreshChildrens() {
		children.clear();
		File[] files;
		if ((files = file.listFiles()) != null) {
			for (File f : files) {
				if (!f.isHidden()) {
					children.add(f);
				}
			}
		}
	}
}
